package com.richman.controller;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Map;
import java.util.Vector;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

import org.json.JSONArray;
import org.json.JSONObject;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.richman.hg.HGHttp;
import com.richman.hg.HGManager;
import com.richman.isn.ISNManager;
import com.richman.ps38.PS38Http;
import com.richman.ps38.PS38Manager;
import com.richman.ps38.entity.BetAdyData;
import com.richman.ps38.entity.BetDvalue;
import com.richman.ps38.entity.Event;
import com.richman.ps38.entity.HGEvent;
import com.richman.ps38.entity.ISNHGSendData;
import com.richman.ps38.entity.SendData;

public class BetCentralManager {
	
	
	public HGManager hgmanager = null;
	
	public PS38Manager ps38manager = null;
	
	//public PS38Http ps38http = new PS38Http();

	public Vector<BetAdyData> betAdyDatas = new Vector<BetAdyData>();
	
	public AutoBetCentralManager autobetCtrMng = new AutoBetCentralManager();
	
	public Vector<BetAdyData> betAdyDatasAll = new Vector<BetAdyData>();
	
	
	public Vector<BetAdyData> specialBetAdyDatas = new Vector<BetAdyData>();
	
	public Vector<BetAdyData> specialBetLogDatas = new Vector<BetAdyData>();
	
	public Vector<BetAdyData> betLogDatas = new Vector<BetAdyData>();
	
	public ISNManager isnManager = null;
	
	//public Vector<String> betVec = new Vector<String>(); 

	
	public Vector<BetDvalue> betDvalues = new Vector<BetDvalue>();
	
	public Vector<BetDvalue> betLimitDvalues = new Vector<BetDvalue>();
	
	private  ReadWriteLock lockebetAdyDatas = new ReentrantReadWriteLock();
	
	
	
	private  ReadWriteLock lockeMatches = new ReentrantReadWriteLock();
	public Vector<SendData> matches = new Vector<SendData>();
	
	public long lastbetsuccesstime = System.currentTimeMillis();
	
	Long updateBetlogTime = System.currentTimeMillis();
	
	Long updateSpecialBetlogTime = System.currentTimeMillis();
	
	Long updateps38BetlogTime = System.currentTimeMillis();
	
	Long checkIllegalTime = 0L;//System.currentTimeMillis();
	
	Long getRedcardTime = System.currentTimeMillis();
	
	Long checkSpecialIllegalTime = System.currentTimeMillis();
	
	Long checkGameResTime = 0L;
	
	Long checkDanshiRatioTime = 0L;
	
	public void setSendData(Vector<SendData> datas) {
		try {
			lockeMatches.writeLock().lock();
			matches.clear();
			for(int i = 0; i<datas.size(); i++) {
				matches.add(datas.elementAt(i));	
			}
		}catch(Exception e) {
			e.printStackTrace();
		}
		lockeMatches.writeLock().unlock();

	}
	
	public Vector<SendData> getSendData(){
		return matches;
	}
	
	public void keepISNonline() {
		
		isnManager.login();
		isnManager.getbalance();
	}
	
	public void updateBetAdyTable() {
		NegativeRatioClient.mainWnd.updateBetAdyTable(betAdyDatas);
		//SpecialServer.mainWnd.updateProfitTable(betAdyDatas);
	}
	
	public void updateBetAdyData(BetAdyData bad) {
		
		betAdyDatas.add(bad);
		
		
		NegativeRatioClient.mainWnd.updateBetAdyTable(betAdyDatas);
		//SpecialServer.mainWnd.updateProfitTable(betAdyDatas);
		
		saveBetlistVectofile();
		
	}
	
	public void updateDanshiPankouAndDaxiao(Vector<HGEvent> hgevents) {
		try {
			
			for(int i = 0; i < hgevents.size(); i++) {
				HGEvent hgevent = hgevents.elementAt(i);
				for(int j = 0; j < betAdyDatas.size();j++) {
					if(hgevent.eventId.equals(betAdyDatas.elementAt(j).hgeventid)) {
						betAdyDatas.elementAt(j).danshipankou = hgevent.wholeRQPPankou + "-" + hgevent.wholeRQPPankouUp + "-" + hgevent.wholeRQPPankouDown;
						betAdyDatas.elementAt(j).danshidaxiao = hgevent.wholeDXPPankou + "-" + hgevent.wholeDXPPankouUp + "-" + hgevent.wholeDXPPankouDown;						
					}
					
				}
			}
			
			saveBetlistVectofile();
			
		}catch(Exception e) {
			e.printStackTrace();
		}
		
	}
	
	public void updateBetAdyGameResult(String res) {

   	 	if(res != null) {
   			try{   				
   				Document doc = Jsoup.parse(res);
   				Elements eles = doc.getElementsByTag("tr");

   				for(int i = 0; i < eles.size(); i++) {
   	    			String tmp = eles.get(i).toString();
   	    			if(tmp.contains("acc_result_tr_top") && tmp.contains("acc_result_team")) {
   	    				
   	    				
   	    				
   	    				for(int j = 0; j < betLogDatas.size(); j++) {
   	    					
   	    					if(betLogDatas.elementAt(j).result.contains("-")) {
   	    						continue;//已经获取过了的，就不需要再拿一遍结果了
   	    					}
   	    					if(tmp.contains(betLogDatas.elementAt(j).gid)) {
   	    						Element ele = eles.get(i);
   	    						String homeTeamScore = ele.getElementsByClass("acc_result_full").first().getElementsByTag("span").first().html();
   	    						String awayTeamScore = eles.get(i+1).getElementsByClass("acc_result_full").first().getElementsByTag("span").first().html();
   	    						betLogDatas.elementAt(j).result = homeTeamScore + " - " + awayTeamScore;
   	    					}
   	    					
   	    				}
   	    				
   	    				
   	    				
   	    				
   	    				for(int j = 0; j < betAdyDatas.size();j++) {
   	    					if(betAdyDatas.elementAt(j).result.contains("-")) {
   	    						continue;//已经获取过了的，就不需要再拿一遍结果了
   	    					}
   	    					if(tmp.contains(betAdyDatas.elementAt(j).gid)) {
   	    						Element ele = eles.get(i);
   	    						String homeTeamScore = ele.getElementsByClass("acc_result_full").first().getElementsByTag("span").first().html();
   	    						String awayTeamScore = eles.get(i+1).getElementsByClass("acc_result_full").first().getElementsByTag("span").first().html();
   	    						betAdyDatas.elementAt(j).result = homeTeamScore + " - " + awayTeamScore;
   	    					}
   	    				}
   	    			}
   	    			
   	    		}
   				
   				
   				
   				
   				for(int i = 0; i < betLogDatas.size(); i++) {
   					
   					if(!betLogDatas.elementAt(i).result.contains("-")) {
   						
   						for(int j = 0; j < betAdyDatas.size(); j++) {
   							if(betAdyDatas.elementAt(j).hgeventid.equals(betLogDatas.elementAt(i).hgeventid)) {
   								betLogDatas.elementAt(i).result = betAdyDatas.elementAt(j).result;
   								break;
   							}
   						}
   						
   					}
   					
   					
   				}
   				
   				
   		     	NegativeRatioClient.mainWnd.updateBetAdyTable(betAdyDatas);
   		     	//SpecialServer.mainWnd.updateProfitTable(betAdyDatas);
   	   			saveBetlistVectofile();
   	   			
   	   			
   	   		NegativeRatioClient.mainWnd.updateBetLogTable(betLogDatas);
   	   		
   	   		saveBetLogVectofile();
   	   			
   	   			
   				
   			}catch(Exception e) {
   				e.printStackTrace();
   			}
     	}   	 	
	}
	
	
	public void updateBetAdyGameResultUsePs38(String dataStr) {
		
		try {
			
			if(dataStr != null) {
				JSONArray ja = new JSONArray(dataStr);
				
				//{ "序号", "详细信息",  "选择队伍", "赔率", "注额", "输赢", "佣金", "状态"};
				
				for(int i = 0; i < ja.length(); i++) {
					
					JSONArray ja1 = ja.getJSONArray(i);
					
					String wagerId = Long.toString(ja1.getLong(7));
					
		     		boolean find = false;
		       	 	for(int j = 0;j<betAdyDatas.size();j++) {
		       	 		
		       	   	 	//System.out.println("result:" + betAdyDatas.elementAt(i).result);
		       	   	 	
		       	   	 		if(betAdyDatas.elementAt(j).result.equals("null")|| 
		       	   	 				betAdyDatas.elementAt(j).result.equals("PS38")||
		       	   	 			betAdyDatas.elementAt(j).result.equals("ISN") ||
		       	   	 		!betAdyDatas.elementAt(j).result.contains("-")
		       	   	 				) {
		       	   	 			if(betAdyDatas.elementAt(j).result.equals("PS38")||
		       	   	    	 			betAdyDatas.elementAt(j).result.equals("ISN")) {
		       	   	 			betAdyDatas.elementAt(j).result = "null";
		       	   	 			}
		       	   	 			
		       	   	 			if(wagerId.contains(betAdyDatas.elementAt(j).wagerId)) {
		       	   	 				
			       	 				String scoreStr = ja1.getString(4);
			       					String[] scoreArry = scoreStr.split(",");
			       					
			       					String wholeScore = "null";
			       					
			       					if(scoreArry.length >1) {		       					
			       						wholeScore = scoreArry[1].split(":")[1];
			       						betAdyDatas.elementAt(j).result = wholeScore;	
			       						
			       					}
			       					find = true;

		       	   	 			}
		       	   	 			
		       	   	 			if(find==true) {
			   	 					break;
			   	 				}   
		       	   	 		}
		       	   	 	}

				}
				
		     	NegativeRatioClient.mainWnd.updateBetAdyTable(betAdyDatas);
		     	//SpecialServer.mainWnd.updateProfitTable(betAdyDatas);
	   			saveBetlistVectofile();
			}
			
			
		}catch(Exception e) {
			
		}
		


	}
	
	public void getRedcard() {
		try {
			if(System.currentTimeMillis() - getRedcardTime < 30*1000 ) {
				return;
			}
			getRedcardTime = System.currentTimeMillis();
			hgmanager.getRedCard();
		}catch(Exception e) {
			e.printStackTrace();
		}
	}

	public void updateSpecialBetAdyData(BetAdyData bad) {
		
		specialBetAdyDatas.add(bad);

		NegativeRatioClient.mainWnd.updateSpecialBetAdyTable(specialBetAdyDatas);
		
		saveSpecialBetlistVectofile();
		
	}

	
	public void updateSpecialBetLogData(BetAdyData bad) {
		
		specialBetLogDatas.add(bad);

		NegativeRatioClient.mainWnd.updateSpecialBetLogTable(specialBetLogDatas);
		
		saveSpecialBetlogVectofile();
		
	}
	
	
	public void updateBetLogData(BetAdyData bad) {
		
		betLogDatas.add(bad);
		
		
		NegativeRatioClient.mainWnd.updateBetLogTable(betLogDatas);
		
		NegativeRatioClient.mainWnd.updateBetLimitTable(betLogDatas);
		
		saveBetLogVectofile();
		
	}
	
	public void updateBetLog(String betID, String statusCode, String betAdyAmount) {
		for(int i = 0;i < betLogDatas.size(); i++) {
			BetAdyData data = betLogDatas.elementAt(i);
			if(statusCode.equals("104")) {
				System.out.println();
			}
			if(data.gid.equals(betID)&&data.websitetype.contains("betCode")) {
				betLogDatas.elementAt(i).ps38betres = statusCode;
				if(statusCode.equals("101")||statusCode.equals("200")||statusCode.equals("106")) {
					betLogDatas.elementAt(i).ps38betgold = betAdyAmount;
				}
				break;
			}
		}
		
		NegativeRatioClient.mainWnd.updateBetLogTable(betLogDatas);
		saveBetLogVectofile();
	}
	
	
	public void clearBetAdyData() {
		
		if(betAdyDatas!= null && betAdyDatas.size() != 0) {
			betAdyDatas.clear();
			
			NegativeRatioClient.mainWnd.updateBetAdyTable(betAdyDatas);
			//SpecialServer.mainWnd.updateProfitTable(betAdyDatas);
			
			
			saveBetlistVectofile();
		}
		
	}
	
	
	public void clearBetLogData() {
		
		if(betLogDatas!= null && betLogDatas.size() != 0) {
			betLogDatas.clear();
			
			NegativeRatioClient.mainWnd.updateBetLogTable(betLogDatas);
			
			NegativeRatioClient.mainWnd.updateBetLimitTable(betLogDatas);
			
			saveBetLogVectofile();
		}
		
	}
	
	
	public void clearSpecialBetAdyData() {
		if(specialBetAdyDatas!= null && specialBetAdyDatas.size() != 0) {
			specialBetAdyDatas.clear();
			
			NegativeRatioClient.mainWnd.updateSpecialBetAdyTable(specialBetAdyDatas);
			
			saveSpecialBetlistVectofile();
		}
	}
	
	
	public void clearSpecialBetLogData() {
		
		if(specialBetLogDatas!= null && specialBetLogDatas.size() != 0) {
			specialBetLogDatas.clear();
			
			NegativeRatioClient.mainWnd.updateSpecialBetLogTable(specialBetLogDatas);
			
			saveSpecialBetlogVectofile();
		}
		
	}
	
	
	public Vector<BetAdyData> getBetAdyData(){
		return betAdyDatas;
	}
	
	
	public int checkWholeDXQBetGold(SendData match) {
		try {
			String leaguename = match.isnps38event.leagueName;
			String teams = match.isnps38event.homeTeam + "-vs-" + match.isnps38event.awayTeam;
			String time = match.isnps38event.eventTime;
			String pankou = match.isnps38event.wholeDXPPankou;
			String betstr = leaguename + teams + time + pankou;
			int alreadyBetGold = 0;
			for(int i = 0; i < betAdyDatas.size(); i++) {
				BetAdyData betAdyData = betAdyDatas.elementAt(i);
				String tmpstr = betAdyData.league + betAdyData.teams + betAdyData.time + betAdyData.betpankou;
				if(tmpstr.equals(betstr)&&(betAdyData.ps38betres.equals("0")||betAdyData.ps38betres.equals("1")||betAdyData.ps38betres.equals("2"))&&betAdyData.bettype.contains("全场")) {
					int betgold = Integer.parseInt(betAdyData.ps38betgold);
					alreadyBetGold += betgold;
				}				
			}
			return alreadyBetGold;
		}catch(Exception e) {
			e.printStackTrace();
		}		
		return -1;
	}
	
	
	public int checkHalfDXQBetGold(SendData match) {
		try {
			String leaguename = match.isnps38event.leagueName;
			String teams = match.isnps38event.homeTeam + "-vs-" + match.isnps38event.awayTeam;
			String time = match.isnps38event.eventTime;
			String pankou = match.isnps38event.halfDXPPankou;
			String betstr = leaguename + teams + time + pankou;
			int alreadyBetGold = 0;
			for(int i = 0; i < betAdyDatas.size(); i++) {
				BetAdyData betAdyData = betAdyDatas.elementAt(i);
				String tmpstr = betAdyData.league + betAdyData.teams + betAdyData.time + betAdyData.betpankou;
				if(tmpstr.equals(betstr)&&(betAdyData.ps38betres.equals("0")||betAdyData.ps38betres.equals("1")||betAdyData.ps38betres.equals("2"))&&betAdyData.bettype.contains("半场")) {
					int betgold = Integer.parseInt(betAdyData.ps38betgold);
					alreadyBetGold += betgold;
				}
			}
			return alreadyBetGold;
		}catch(Exception e) {
			e.printStackTrace();
		}		
		return -1;
	}
	

	public int checkBetGold(SendData match, String bettype, String betID) {
		try {
//			String leaguename = match.isnps38event.leagueName;
//			String teams = match.isnps38event.homeTeam + "-vs-" + match.isnps38event.awayTeam;
//			String time = match.isnps38event.eventTime;
//			String pankou = "";
//			if(bettype.contains("全场让球")) {
//				pankou = match.isnps38event.wholeRQPPankou;
//			}else if(bettype.contains("半场让球")) {
//				pankou = match.isnps38event.halfRQPPankou;
//			}else if(bettype.contains("全场大小")) {
//				pankou = match.isnps38event.wholeDXPPankou;
//			}else if(bettype.contains("半场大小")) {
//				pankou = match.isnps38event.halfDXPPankou;
//			}
//			
//			String betstr = leaguename + teams + time + pankou + bettype;
			int alreadyBetGold = 0;
			for(int i = 0; i < betLogDatas.size(); i++) {
				BetAdyData betAdyData = betLogDatas.elementAt(i);
				//String tmpstr = betAdyData.league + betAdyData.teams + betAdyData.time + betAdyData.betpankou + betAdyData.bettype;
				if(betID.equals(betLogDatas.elementAt(i).gid)&&!betAdyData.websitetype.contains("betCode")&&(betAdyData.ps38betres.equals("1")||betAdyData.ps38betres.equals("2"))) {
					int betgold = Integer.parseInt(betAdyData.ps38betgold);
					alreadyBetGold += betgold;
				}
			}
			return alreadyBetGold;
		}catch(Exception e) {
			e.printStackTrace();
		}		
		return -1;
	}

	
	public boolean checkNeddConfirm(String betID) {
		try {

			for(int i = 0; i < betAdyDatas.size(); i++) {
				BetAdyData betAdyData = betAdyDatas.elementAt(i);
				//String tmpstr = betAdyData.league + betAdyData.teams + betAdyData.time + betAdyData.betpankou + betAdyData.bettype;
				if(betID.equals(betAdyDatas.elementAt(i).gid)&&!betAdyData.websitetype.contains("betCode")&&(betAdyData.ps38betres.equals("0"))) {
					return true;
				}
			}

		}catch(Exception e) {
			e.printStackTrace();
		}		
		return false;
	}
	
	
	
	public double findFirstWholeDXQPankou(SendData match) {
		try {
			String leaguename = match.isnps38event.leagueName;
			String teams = match.isnps38event.homeTeam + "-vs-" + match.isnps38event.awayTeam;
			String time = match.isnps38event.eventTime;
			String betstr = leaguename + teams + time;			
			for(int i = 0; i < betAdyDatas.size(); i++) {
				BetAdyData betAdyData = betAdyDatas.elementAt(i);
				String tmpstr = betAdyData.league + betAdyData.teams + betAdyData.time;
				if(tmpstr.equals(betstr)&&(betAdyData.ps38betres.equals("1")||betAdyData.ps38betres.equals("2"))&&betAdyData.bettype.contains("全场")) {
					return Double.parseDouble(betAdyData.betpankou);
				}
			}
		}catch(Exception e) {
			e.printStackTrace();
		}
		return 0.0;
	}
	
	public double findFirstHalfDXQPankou(SendData match) {
		try {
			String leaguename = match.isnps38event.leagueName;
			String teams = match.isnps38event.homeTeam + "-vs-" + match.isnps38event.awayTeam;
			String time = match.isnps38event.eventTime;
			String betstr = leaguename + teams + time;			
			for(int i = 0; i < betAdyDatas.size(); i++) {
				BetAdyData betAdyData = betAdyDatas.elementAt(i);
				String tmpstr = betAdyData.league + betAdyData.teams + betAdyData.time;
				if(tmpstr.equals(betstr)&&(betAdyData.ps38betres.equals("1")||betAdyData.ps38betres.equals("2"))&&betAdyData.bettype.contains("半场")) {
					return Double.parseDouble(betAdyData.betpankou);
				}
			}
		}catch(Exception e) {
			e.printStackTrace();
		}
		return 0.0;
	}
	
	public Vector<BetAdyData> getBetLogData(){
		return betLogDatas;
	}
	
	
	public  boolean recoverBetlistVecfromefile(){
		
		try{
			
			SimpleDateFormat dfmin = new SimpleDateFormat("yyyy-MM-dd");// 设置日期格式
			
			Calendar currentTime = Calendar.getInstance();
			
			String folder = "";
			
//			if(currentTime.get(Calendar.HOUR_OF_DAY) >= 12){
//				folder += dfmin.format(currentTime.getTimeInMillis());
//			}else{
//				currentTime.add(Calendar.DAY_OF_MONTH, -1);
//				folder += dfmin.format(currentTime.getTimeInMillis());
//			}
			
			
			//读取改目录下所有文件:
			File filefolder = new File("betdata/");
			
			if(!filefolder.exists()){
				return true;
			}
			
			File flist[] = filefolder.listFiles();
			
			int linenum = 0;
			
			for(File f : flist){
				if(f.getName().equals("betres.data")){
//					System.out.println("fuck directory here");
//				}else{
					//System.out.println(f.getAbsolutePath());
					//
					
					
					BufferedReader freader = new BufferedReader(new InputStreamReader(new FileInputStream(f), "UTF-8"));
					
					String str = "";
					
					linenum = 0;
					
					while ((str = freader.readLine()) != null) {
						
						
						BetAdyData bad = new BetAdyData(str);
						
						betAdyDatas.add(bad);
						
							
						}
					
					
					
					
					if(null != freader){
						freader.close();
					}
					
					
				}
				
				
				
				
			}
			
			
			NegativeRatioClient.mainWnd.updateBetAdyTable(betAdyDatas);
			//SpecialServer.mainWnd.updateProfitTable(betAdyDatas);
			
			return true;
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
		
		
	}
	
	
	
	
	
	public  boolean recoverAllBetlistVecfromefile(){
		
		try{
			

			
			//读取改目录下所有文件:
			File filefolder = new File("betdata/");
			
			if(!filefolder.exists()){
				return true;
			}
			
			File flist[] = filefolder.listFiles();

			
			for(File f : flist){
				if(f.getName().equals("betres.data")){

					BufferedReader freader = new BufferedReader(new InputStreamReader(new FileInputStream(f), "UTF-8"));
					
					String str = "";

					
					while ((str = freader.readLine()) != null) {
						
						
						BetAdyData bad = new BetAdyData(str);
						
						betAdyDatasAll.add(bad);
						
							
						}
					
					
					
					
					if(null != freader){
						freader.close();
					}
					
					
				}
				
				
				
				
			}
			
			
			
			
			File fileotherfolder = new File("otherbetdata/");
			
			if(fileotherfolder.exists()){
				
				File otherflist[] = fileotherfolder.listFiles();
				
			
				
				for(File f : otherflist){
					if(f.getName().equals("betres.data")){

						
						BufferedReader freader = new BufferedReader(new InputStreamReader(new FileInputStream(f), "UTF-8"));
						
						String str = "";
						
			
						
						while ((str = freader.readLine()) != null) {
							
							
							BetAdyData bad = new BetAdyData(str);
							
							betAdyDatasAll.add(bad);
							
								
							}
						
						
						
						
						if(null != freader){
							freader.close();
						}
						
						
					}
					
					
					
					
				}
				
			}
			

			
			
			
			
			
			NegativeRatioClient.mainWnd.updateProfitTable(betAdyDatasAll);
			
			return true;
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
		
		
	}
	
	
	
	
	
	
	

	
	

	public  boolean recoverSpecialBetlistVecfromefile(){
		
		try{
			
			SimpleDateFormat dfmin = new SimpleDateFormat("yyyy-MM-dd");// 设置日期格式
			
			Calendar currentTime = Calendar.getInstance();
			
			String folder = "";
			
//			if(currentTime.get(Calendar.HOUR_OF_DAY) >= 12){
//				folder += dfmin.format(currentTime.getTimeInMillis());
//			}else{
//				currentTime.add(Calendar.DAY_OF_MONTH, -1);
//				folder += dfmin.format(currentTime.getTimeInMillis());
//			}
			
			
			//读取改目录下所有文件:
			File filefolder = new File("betdata/");
			
			if(!filefolder.exists()){
				return true;
			}
			
			File flist[] = filefolder.listFiles();
			
			int linenum = 0;
			
			for(File f : flist){
				if(f.getName().equals("specialbetres.data")){
//					System.out.println("fuck directory here");
//				}else{
					//System.out.println(f.getAbsolutePath());
					//
					
					
					BufferedReader freader = new BufferedReader(new InputStreamReader(new FileInputStream(f), "UTF-8"));
					
					String str = "";
					
					linenum = 0;
					
					while ((str = freader.readLine()) != null) {
						
						
						BetAdyData bad = new BetAdyData(str);
						
						specialBetAdyDatas.add(bad);
						
							
						}
					
					
					
					
					if(null != freader){
						freader.close();
					}
					
					
				}
				
				
				
				
			}
			
			
			NegativeRatioClient.mainWnd.updateSpecialBetAdyTable(specialBetAdyDatas);
			
			return true;
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
		
		
	}
	
	public String getOutlineData() {
		return isnManager.getOutlineData()
				;
	}
	
	public String getDetailsData(String datetime) {
		return isnManager.getDetailsData(datetime);
	}
	
	public String getPS38OutlineData(String count) {
		return ps38manager.getOutlineData(count)
				;
	}
	
	public String getPS38DetailsData(String datetime) {
		return ps38manager.getDetailsData(datetime);
	}
	
	public String getHGDetailsData(Integer index,String datetime) {
		return hgmanager.getDetailsData(index, datetime);
	}

	public String getHGOutlineData(Integer index) {
		return hgmanager.getOutlineData(index)
				;
	}

	public  boolean recoverSpecialBetlogVecfromefile(){
		
		try{
			
			SimpleDateFormat dfmin = new SimpleDateFormat("yyyy-MM-dd");// 设置日期格式
			
			Calendar currentTime = Calendar.getInstance();
			
			String folder = "";

			
			
			//读取改目录下所有文件:
			File filefolder = new File("betdata/");
			
			if(!filefolder.exists()){
				return true;
			}
			
			File flist[] = filefolder.listFiles();
			
			int linenum = 0;
			
			for(File f : flist){
				if(f.getName().equals("specialbetlog.data")){
//					System.out.println("fuck directory here");
//				}else{
					//System.out.println(f.getAbsolutePath());
					//
					
					
					BufferedReader freader = new BufferedReader(new InputStreamReader(new FileInputStream(f), "UTF-8"));
					
					String str = "";
					
					linenum = 0;
					
					while ((str = freader.readLine()) != null) {
						
						
						BetAdyData bad = new BetAdyData(str);
						
						specialBetLogDatas.add(bad);
						
							
						}
					
					
					
					
					if(null != freader){
						freader.close();
					}
					
					
				}
				
				
				
				
			}
			
			
			NegativeRatioClient.mainWnd.updateSpecialBetLogTable(specialBetLogDatas);
			
			return true;
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
		
		
	}
	
	
	
	public  boolean recoverBetLogVecfromfile(){
		
		try{
			
			SimpleDateFormat dfmin = new SimpleDateFormat("yyyy-MM-dd");// 设置日期格式
			
			Calendar currentTime = Calendar.getInstance();
			
			String folder = "";
			
//			if(currentTime.get(Calendar.HOUR_OF_DAY) >= 12){
//				folder += dfmin.format(currentTime.getTimeInMillis());
//			}else{
//				currentTime.add(Calendar.DAY_OF_MONTH, -1);
//				folder += dfmin.format(currentTime.getTimeInMillis());
//			}
			
			
			//读取改目录下所有文件:
			File filefolder = new File("betdata/");
			
			if(!filefolder.exists()){
				return true;
			}
			
			File flist[] = filefolder.listFiles();
			
			int linenum = 0;
			
			for(File f : flist){
				
				//System.out.println(f.getName());
				
				if(f.getName().contains("betlog")){
//					System.out.println("fuck directory here");
//				}else{
					//System.out.println(f.getAbsolutePath());
					//
					
					
					BufferedReader freader = new BufferedReader(new InputStreamReader(new FileInputStream(f), "UTF-8"));
					
					String str = "";
					
					linenum = 0;
					
					while ((str = freader.readLine()) != null) {
						
						
						BetAdyData bad = new BetAdyData(str);
						
						if(bad.websitetype.contains("betCode")
								&&!bad.ps38betres.equals("200")
								&&!bad.ps38betres.equals("109")
								&&!bad.ps38betres.equals("104")) {
							bad.ps38betres = "109";
						}
						
						betLogDatas.add(bad);
						
						
							
						}
					
					
					
					
					if(null != freader){
						freader.close();
					}
					
					
				}
				
				
				
				
			}
			
			
			NegativeRatioClient.mainWnd.updateBetLogTable(betLogDatas);
			
			NegativeRatioClient.mainWnd.updateBetLimitTable(betLogDatas);
			
			return true;
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
		
		
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	public  boolean saveBetlistVectofile(){
		try{
			
			SimpleDateFormat dfmin = new SimpleDateFormat("yyyy-MM-dd");// 设置日期格式
			
			Calendar currentTime = Calendar.getInstance();
			
			String folder = "";
			
			File filefolder = new File("betdata/");
			
			if(!filefolder.exists()){
				filefolder.mkdir();
			}
			
//			if(currentTime.get(Calendar.HOUR_OF_DAY) >= 12){
//				folder += dfmin.format(currentTime.getTimeInMillis());
//			}else{
//				currentTime.add(Calendar.DAY_OF_MONTH, -1);
//				folder += dfmin.format(currentTime.getTimeInMillis());
//			}
			
//			filefolder = new File("betdata/" + folder + "/");
//			
//			if(!filefolder.exists()){
//				filefolder.mkdir();
//			}
			
			String filenametmp = "betres"
					+ ".data";
			
			
			
			
			
			File file = new File("betdata/" +filenametmp);
			
			if(!file.exists()){
				System.out.println("create betdata/" + filenametmp);
				
				
				
				file.createNewFile();
			}
			
			BufferedWriter fwlocal = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, false), "UTF-8"));
			
			
			
			
			for(int i = 0; i < betAdyDatas.size(); i++){
				
				BetAdyData item = betAdyDatas.elementAt(i);
				
				String str = item.toString();
				
				fwlocal.append(str);
				fwlocal.newLine();
				fwlocal.flush();

			}
			
			fwlocal.close();
			
			
			
			return true;
			
			
		}catch(Exception e){
			e.printStackTrace();
			
			
			
			
			return false;
		}
	}
	

	
	
	public  boolean saveSpecialBetlistVectofile(){
		try{
			
			SimpleDateFormat dfmin = new SimpleDateFormat("yyyy-MM-dd");// 设置日期格式
			
			Calendar currentTime = Calendar.getInstance();
			
			String folder = "";
			
			File filefolder = new File("betdata/");
			
			if(!filefolder.exists()){
				filefolder.mkdir();
			}
			
			String filenametmp = "specialbetres"
					+ ".data";
			
			
			
			
			
			File file = new File("betdata/" +filenametmp);
			
			if(!file.exists()){
				System.out.println("create betdata/" + filenametmp);
				
				
				
				file.createNewFile();
			}
			
			BufferedWriter fwlocal = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, false), "UTF-8"));
			
			
			
			
			for(int i = 0; i < specialBetAdyDatas.size(); i++){
				
				BetAdyData item = specialBetAdyDatas.elementAt(i);
				
				String str = item.toString();
				
				fwlocal.append(str);
				fwlocal.newLine();
				fwlocal.flush();

			}
			
			fwlocal.close();
			
			
			
			return true;
			
			
		}catch(Exception e){
			e.printStackTrace();			
			return false;
		}
	}
	
	
	
	public  boolean saveSpecialBetlogVectofile(){
		
		try{
			
			SimpleDateFormat dfmin = new SimpleDateFormat("yyyy-MM-dd");// 设置日期格式
			
			Calendar currentTime = Calendar.getInstance();
			
			String folder = "";
			
			File filefolder = new File("betdata/");
			
			if(!filefolder.exists()){
				filefolder.mkdir();
			}
			
			String filenametmp = "specialbetlog"
					+ ".data";
			
			
			
			
			
			File file = new File("betdata/" +filenametmp);
			
			if(!file.exists()){
				System.out.println("create betdata/" + filenametmp);
				
				
				
				file.createNewFile();
			}
			
			BufferedWriter fwlocal = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, false), "UTF-8"));
			
			
			
			
			for(int i = 0; i < specialBetLogDatas.size(); i++){
				
				BetAdyData item = specialBetLogDatas.elementAt(i);
				
				String str = item.toString();
				
				fwlocal.append(str);
				fwlocal.newLine();
				fwlocal.flush();

			}
			
			fwlocal.close();
			
			
			
			return true;
			
			
		}catch(Exception e){
			e.printStackTrace();			
			return false;
		}
	}
	
	
	
	public  boolean saveBetLogVectofile(){
		try{
			
			SimpleDateFormat dfmin = new SimpleDateFormat("yyyy-MM-dd");// 设置日期格式
			
			Calendar currentTime = Calendar.getInstance();
			
			String folder = "";
			
			File filefolder = new File("betdata/");
			
			if(!filefolder.exists()){
				filefolder.mkdir();
			}
			
//			if(currentTime.get(Calendar.HOUR_OF_DAY) >= 12){
//				folder += dfmin.format(currentTime.getTimeInMillis());
//			}else{
//				currentTime.add(Calendar.DAY_OF_MONTH, -1);
//				folder += dfmin.format(currentTime.getTimeInMillis());
//			}
			
//			filefolder = new File("betdata/" + folder + "/");
//			
//			if(!filefolder.exists()){
//				filefolder.mkdir();
//			}
			
			String filenametmp = "betlog"
					+ ".data";
			
			
			
			
			
			File file = new File("betdata/" +filenametmp);
			
			if(!file.exists()){
				System.out.println("create betdata/" + filenametmp);
				
				
				
				file.createNewFile();
			}
			
			BufferedWriter fwlocal = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, false), "UTF-8"));
			
			
			
			
			for(int i = 0; i < betLogDatas.size(); i++){
				
				BetAdyData item = betLogDatas.elementAt(i);
				
				String str = item.toString();
				
				fwlocal.append(str);
				fwlocal.newLine();
				fwlocal.flush();

			}
			
			fwlocal.close();
			
			
			
			return true;
			
			
		}catch(Exception e){
			e.printStackTrace();
			
			
			
			
			return false;
		}
	}
	
	
	
	
	public void updateBetCentralTable(Vector<SendData> senddatas, Vector<ISNHGSendData> isnhgsenddatas) {
		
		NegativeRatioClient.mainWnd.updateBetCentralTable(senddatas, isnhgsenddatas);
		
	}
	
	public BetCentralManager() {
		hgmanager = new HGManager();
		hgmanager.init();
		
		ps38manager = new PS38Manager();
		ps38manager.init();
		
		isnManager = new ISNManager();
		isnManager.init();
		
//		recoverBetlistVecfromefile();
//		
//		recoverBetLogVecfromfile();
		
	}
	
	public String getinUsedAccountName() {
		return hgmanager.getinUsedAccountName();
	}
	
	public void useNextSpecialAccount() {
		hgmanager.useNextSpecialAccount();
	}

	public void useNextAccount() {
		hgmanager.useNextAccount();
	}

	
//	public Vector<String> getaccountnames(){
//		return hgmanager.getaccountnames();
//	}
	
	//HG
	public void keepHGonline() {
		hgmanager.login();
		hgmanager.checkIsOnline();
	};
	
	
	
	public Map<String, String> getInplaybetScores(){
		
		if(hgmanager.getStatuscode() == 1) {
			return hgmanager.getInplaybetScores();
		}
		return null;
		
	}
	
	
	
	
	public Map<String, String> updateRatio(String gid, String type, String gnum, String strong, String rtype, String wtype){
		
		if(hgmanager.getStatuscode() == 1) {
			return hgmanager.updateRatio(gid, type, gnum, strong, rtype, wtype);
		}
		return null;
		
	}
	
	
	public Map<String, String> updateSpecialAccountRatio(String gid, String type, String gnum, String strong, String rtype, String wtype){
		
		if(hgmanager.getSpecialAccountStatuscode() == 1) {
			return hgmanager.updateSpecialAccountRatio(gid, type, gnum, strong, rtype, wtype);
		}
		return null;
		
	}
	
	
	
	public Map<String,String> hgdobet(Map<String, String> betParam, int gold) {
		
		if(hgmanager.getStatuscode()==1) {
			return hgmanager.dobet(betParam , gold);
		}
		return null;
	}

	
	public Map<String, String> isndobet(String homeTeam, String awayTeam, String updown, String bettype, int gold, Double pankouarg,double ratio) {
		
		if(isnManager.getStatuscode()==1) {
			return isnManager.dobet( homeTeam,  awayTeam,  updown,  bettype,  gold,  pankouarg, ratio);
		}
		return null;
	}
	
	public String isndobetdxq(String homeTeam, String awayTeam, String updown, double ratio, int gold, Vector<Double> hgpankous) {
		
		if(isnManager.getStatuscode()==1) {
			return isnManager.dobetdxq(homeTeam, awayTeam, updown, ratio, gold, hgpankous);
		}
		return null;
	}
	
	public JSONObject getISNEvent() {
		if(isnManager.getStatuscode()==1) {
			return isnManager.getISNEvent();
		}
		return null;
	}
	
	public Map<String,String> hgdospecialbet(String betParam, int gold) {
		
		if(hgmanager.getSpecialAccountStatuscode()==1) {
			return hgmanager.dobetSpecialaccount(betParam, gold);
		}
		return null;
	}

	
	
	public Map<String, String> ps38dobet(Map<String, String> oddUpdres, int gold) {
		if(ps38manager.getStatuscode()==2) {
			return ps38manager.dobet(oddUpdres , gold);
		}
		return null;
	}
	
	
	
	public Map<String, String> manualps38dobet(Map<String, String> oddUpdres, int gold) {
		if(ps38manager.getStatuscode()==2) {
			return ps38manager.manualdobet(oddUpdres , gold);
		}
		return null;
	}
	
	
	
	//PS38
	public void keepPS38online() {
		ps38manager.prepareLogin();
		ps38manager.getMainPage();
		ps38manager.login();
		ps38manager.getbalance();
	}
	

	
	
	public Map<String, String>  updatePS38Ratio(String betid, String eventid, String bettype, String typedetail, String pankou) {
		
		if(ps38manager.getStatuscode() == 2) {
			return ps38manager.updatePS38Ratio(betid, eventid, bettype, typedetail, pankou);
		}
		
		return null;
	}
	
	
	public Map<String, String>  updateISNRatio(String homeTeam, String awayTeam, String updown, String bettype,  Double pankouarg) {
		
		if(isnManager.getStatuscode() == 1) {
			return isnManager.updateISNRatio( homeTeam,  awayTeam,  updown,  bettype,   pankouarg);
		}
		
		return null;
	}
	
	
	public void updatePS38StatusCode(int i) {
		ps38manager.updateStatuscode(i);
	}
	
	
	
	public Map<String, String>  manualRebetUpdatePS38Ratio(String betid, String eventid, String bettype, String typedetail, String pankou) {
		
		if(ps38manager.getStatuscode() == 2) {
			return ps38manager.manualRebetUpdatePS38Ratio(betid, eventid, bettype, typedetail, pankou);
		}
		
		return null;
	}
	
	
	public Event grabOneEvent(String eventidarg, String pankou, String typedetail) {
		if(ps38manager.getStatuscode() == 2) {
			return ps38manager.grabOneEvent(eventidarg, pankou, typedetail);
		}
		
		return null; 
	}
	
	
	
	public void updatehgBetLogData() {
		
		
		try {
			
			
			if(System.currentTimeMillis() - updateBetlogTime < 8*1000) {
				return;
			}
			
			
			updateBetlogTime = System.currentTimeMillis();
			
			
			boolean needToUpdate = false;
			
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
			
			String date = df.format(System.currentTimeMillis());
			
			SimpleDateFormat dfSec = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			
			
			
			
			
			for(int i = 0; i < betLogDatas.size(); i++) {
				
				
				
				BetAdyData bd = betLogDatas.elementAt(i);
				
				if(i != 0) {
					String time =  bd.currenttime;
					
					Long passtime = (System.currentTimeMillis() - dfSec.parse(time).getTime());
					
					if(passtime < 0 || passtime > 60*5 * 1000) {
						continue;
					}
					
				}
				
				
				if(bd.hgbetres.equals("0")) {
					needToUpdate = true;
					break;
				}
			}
			
			if(needToUpdate) {
				hgmanager.updatehgBetLogData(betLogDatas);
				
				NegativeRatioClient.mainWnd.updateBetAdyTable(betAdyDatas);
				//SpecialServer.mainWnd.updateProfitTable(betAdyDatas);
				
				saveBetlistVectofile();
				
				
				NegativeRatioClient.mainWnd.updateBetLogTable(betLogDatas);
				
				NegativeRatioClient.mainWnd.updateBetLimitTable(betLogDatas);
				
				saveBetLogVectofile();
				
			}
			
		}catch(Exception e) {
			e.printStackTrace();
		}
		
		

		
		
	}
	
	
	
	
	
	public void updatehgSpecialBetLogData() {
		
		
		try {
			
			
			if(System.currentTimeMillis() - updateSpecialBetlogTime < 8*1000) {
				return;
			}
			
			
			updateSpecialBetlogTime = System.currentTimeMillis();
			
			
			boolean needToUpdate = false;
			
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
			
			String date = df.format(System.currentTimeMillis());
			
			SimpleDateFormat dfSec = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			
			
			
			
			
			for(int i = 0; i < specialBetLogDatas.size(); i++) {
				
				
				
				BetAdyData bd = specialBetLogDatas.elementAt(i);
				
				if(i != 0) {
					String time =  bd.currenttime;
					
					Long passtime = (System.currentTimeMillis() - dfSec.parse(time).getTime());
					
					if(passtime < 0 || passtime > 60*5 * 1000) {
						continue;
					}
					
				}
				
				
				if(bd.hgbetres.equals("0")) {
					needToUpdate = true;
					break;
				}
			}
			
			if(needToUpdate) {
				hgmanager.updatehgSpecialAccountBetLogData(specialBetLogDatas);
				
				NegativeRatioClient.mainWnd.updateSpecialBetAdyTable(specialBetAdyDatas);
				
				saveSpecialBetlistVectofile();
				
				
				NegativeRatioClient.mainWnd.updateBetLogTable(specialBetLogDatas);
				
				NegativeRatioClient.mainWnd.updateBetLimitTable(betLogDatas);
				
				saveSpecialBetlogVectofile();
				
			}
			
		}catch(Exception e) {
			e.printStackTrace();
		}

	}
	
	
	
	public void udpateps38BetLogData() {
		
		try{
			
			
			if(System.currentTimeMillis() - updateps38BetlogTime < 8*1000 || NegativeRatioClient.isInbet == true) {
				return;
			}
			
			
			updateps38BetlogTime = System.currentTimeMillis();
			
			boolean needToUpdate = false;
			
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
			
			String date = df.format(System.currentTimeMillis());
			
			SimpleDateFormat dfSec = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			

			
			for(int i = 0; i < betLogDatas.size(); i++) {
				
				
				
				BetAdyData bd = betLogDatas.elementAt(i);
				
				if(i != 0) {
					String time = bd.currenttime;
					
					Long passtime = (System.currentTimeMillis() - dfSec.parse(time).getTime());
					
					if(passtime < 0 || passtime > 60*5 * 1000) {
						continue;
					}
					
				}
				
				
				if(bd.ps38betres.equals("0")&&bd.websitetype.equals("PS38")) {
					needToUpdate = true;
					break;
				}
			}
			
			if(needToUpdate) {
				ps38manager.updateps38BetLogData(betLogDatas);
				NegativeRatioClient.mainWnd.updateBetAdyTable(betAdyDatas);
				//SpecialServer.mainWnd.updateProfitTable(betAdyDatas);
				
				saveBetlistVectofile();
				
				
				NegativeRatioClient.mainWnd.updateBetLogTable(betLogDatas);
				
				NegativeRatioClient.mainWnd.updateBetLimitTable(betLogDatas);
				
				saveBetLogVectofile();
				
			}
			
			
			
			
			
		}catch(Exception e) {
			e.printStackTrace();
		}
		
		
	}


	public void checkIllegalBet() {
		try {
			
			if(System.currentTimeMillis() - checkIllegalTime < 30*60*1000 || NegativeRatioClient.hasIllegalBet == true) {
				return;
			}
			
			checkIllegalTime = System.currentTimeMillis();
			
			hgmanager.checkIllegalBet();
			
			//hgmanager.getRedCard();

		}catch(Exception e) {
			e.printStackTrace();
		}
		
	}
	
	
	public void checkSpecialIllegalBet() {
		try {
			
			if(System.currentTimeMillis() - checkSpecialIllegalTime < 60*1000 || NegativeRatioClient.hasSpecialIllegalBet == true) {
				return;
			}
			
			checkSpecialIllegalTime = System.currentTimeMillis();
			
			hgmanager.checkSpecialIllegalBet();

		}catch(Exception e) {
			e.printStackTrace();
		}
		
	}
	
	public void getDanshiPankouAndDaxiao() {
		try {			
			if(System.currentTimeMillis() - checkDanshiRatioTime < 60*1*1000 ) {
				return;
			}			
			checkDanshiRatioTime = System.currentTimeMillis();			
			hgmanager.getDanshiPankouAndDaxiao();

			
		}catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	public void getgameresult() {
		try {			
			if(System.currentTimeMillis() - checkGameResTime < 60*60*1000 ) {
				return;
			}			
			checkGameResTime = System.currentTimeMillis();			
			hgmanager.getgameresult();
			if(NegativeRatioClient.useForOriginal==false) {
				ps38manager.getgameresult();
			}
			
			
			
		}catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	
	public String getAccounttype() {
		if(hgmanager.getStatuscode() == 1) {
			return hgmanager.getAccounttype();
		}
		return "3";
	}

	public Vector<HGEvent> getSpecialAccountInplaybet() {
		if(hgmanager.getSpecialAccountStatuscode() == 1) {
			return hgmanager.getSpecialAccountInplaybet();
		}
		return null;
	}
	
//	public void startToBet(Vector<SendData> sendDatas) {
//		try {
//			for(int i = 0;  i< sendDatas.size(); i++) {
//        		SendData tmp = sendDatas.elementAt(i);
//        		
//    			String leaguename = tmp.isnevent.leagueName;
//    			String teams = tmp.isnevent.homeTeam + "-vs-" + tmp.isnevent.awayTeam;
//    			String time = tmp.isnevent.eventTime;
//    			String betstr = leaguename + teams + time;
//    			
//    			if(betVec.contains(betstr+ "全场")) {
//    				SpecialServer.betCtrMng.autobetCtrMng.dobetgame("全场大小", "2", tmp);
//    			}
//    			if(betVec.contains(betstr+ "半场")) {
//    				SpecialServer.betCtrMng.autobetCtrMng.dobetgame("半场大小", "2", tmp);
//    			}
//				
//			}
//		}catch(Exception e) {
//			
//		}
//	}

}
